home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Online / opennap / glob.c < prev    next >
C/C++ Source or Header  |  2001-06-08  |  1KB  |  55 lines

  1. /* Copyright (C) 2000-1 drscholl@users.sourceforge.net
  2.    This is free software distributed under the terms of the
  3.    GNU Public License.  See the file COPYING for details.
  4.  
  5.    $Id: glob.c,v 1.4 2001/02/15 08:39:45 drscholl Exp $ */
  6.  
  7. #include <ctype.h>
  8. #include "opennap.h"
  9.  
  10. /* returns >0 if the pattern matches, 0 if the pattern does not match.
  11.  * the match is case-insensitive
  12.  */
  13. int
  14. glob_match (const char *pattern, const char *s)
  15. {
  16.     const char *ptr;
  17.  
  18.     while (*pattern && *s)
  19.     {
  20.     if (*pattern == '*')
  21.     {
  22.         while (*pattern == '*' || *pattern == '?')
  23.         pattern++;
  24.         if (!*pattern)
  25.         {
  26.         /* match to end of string */
  27.         return 1;
  28.         }
  29.         /* recursively attempt to match the rest of the string, using the
  30.          * longest match first
  31.          */
  32.         ptr = s + strlen (s);
  33.         for (;;)
  34.         {
  35.         while (ptr > s && tolower (*(ptr - 1)) != tolower (*pattern))
  36.             ptr--;
  37.         if (ptr == s)
  38.             return 0;    /* no match */
  39.         if (glob_match (pattern + 1, ptr))
  40.             return 1;
  41.         ptr--;
  42.         }
  43.         /* not reached */
  44.     }
  45.     else if (*pattern == '?' || tolower (*pattern) == tolower (*s))
  46.     {
  47.         pattern++;
  48.         s++;
  49.     }
  50.     else
  51.         return 0;        /* no match */
  52.     }
  53.     return ((*pattern || *s) ? 0 : 1);
  54. }
  55.